NDK_PATH=/home/edgarzhou/resource/android-ndk-r21e
CLANG = ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi30-clang 
CROSS_COMPILE=/home/edgarzhou/w/gcc_linaro/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-

CC = ${CROSS_COMPILE}gcc
LD = ${CROSS_COMPILE}ld

DEPS_DIR_PATH=/home/edgarzhou/work/gitee/libbpf-bootstrap-for-android/deps

EXECABLE = monitor-exec

BPFCODE = bpf_program

KERNEL_SRC_PATH := ${HOME}/work/linux/kernel-4.14/

BPFTOOLS = ${KERNEL_SRC_PATH}/samples/bpf
BPFLOADER = $(BPFTOOLS)/bpf_load.c

CCINCLUDE += -I${KERNEL_SRC_PATH}/tools/testing/selftests/bpf -I${KERNEL_SRC_PATH}/usr/include 

LOADINCLUDE += -I${KERNEL_SRC_PATH}/samples/bpf
LOADINCLUDE += -I${KERNEL_SRC_PATH}/tools/lib
LOADINCLUDE += -I${KERNEL_SRC_PATH}/tools/perf
LOADINCLUDE += -I${KERNEL_SRC_PATH}/tools/include
LOADINCLUDE += -I${DEPS_DIR_PATH}/libelf-aarch64/usr/include -I${DEPS_DIR_PATH}/zlib-1.2.10 -I${KERNEL_SRC_PATH}/usr/include 
# LIBRARY_PATH = -L${DEPS_DIR_PATH}/libelf-aarch64/usr/lib -lelf -L${DEPS_DIR_PATH}/zlib-1.2.10 -lz


liba_path = ${DEPS_DIR_PATH}/libelf-aarch64/usr/lib/libelf.a ${DEPS_DIR_PATH}/zlib-1.2.10/libz.a
# BPFSO = ${liba_path} /home/edgarzhou/work/gitee/bootstrap/bpftool/src/libbpf/libbpf.a 
BPFSO = ${liba_path} /home/edgarzhou/work/gitee/bootstrap/old_libbpf/src/libbpf.a
# Setting -DHAVE_ATTR_TEST=0 for the kernel containing below patch:
# 06f84d1989b7 perf tools: Make usage of test_attr__* optional for perf-sys.h
#
# The patch was included in Linus's tree starting v5.5-rc1, but was also included
# in stable kernel branch linux-5.4.y. So it's hard to determine whether a kernel
# is affected based on the kernel version alone:
# - for a v5.4 kernel from Linus's tree, no;
# - for a v5.4 kernel from the stable tree (used by many distros), yes.
#
# So let's look at the actual kernel source code to decide.
#
# See more context at:
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=06f84d1989b7e58d56fa2e448664585749d41221
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fce9501aec6bdda45ef3a5e365a5e0de7de7fe2d
CFLAGS += $(shell grep -q "define HAVE_ATTR_TEST 1" ${KERNEL_SRC_PATH}/tools/perf/perf-sys.h \
                  && echo "-DHAVE_ATTR_TEST=0")

.PHONY: clean $(CLANG) bpfload build

clean:
	rm -f *.o *.so $(EXECABLE)

build: ${BPFCODE.c} ${BPFLOADER}
	$(CLANG) -O2 -target bpf -c $(BPFCODE:=.c) $(CCINCLUDE) -o ${BPFCODE:=.o}

bpfload: build
	${CC} $(CFLAGS) -o $(EXECABLE) $(LOADINCLUDE) $(BPFLOADER) loader.c $(BPFSO) ${liba_path}

$(EXECABLE): bpfload

.DEFAULT_GOAL := $(EXECABLE)
